﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FatoryTool
{
    /// <summary>
    /// FacDesigner.xaml 的交互逻辑
    /// </summary>
    public partial class FacDesigner : Window
    {
        PipeLine pipeLine;
        PipeRender pipeRender;
        public FacDesigner()
        {
            InitializeComponent();

            pipeLine = new PipeLine(30);
            pipeRender = new PipeRender(pipeLine);
        }

        /// <summary>
        /// 清除所有图形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClearAll_Click(object sender, RoutedEventArgs e)
        {
            box.Clip = null;
            win.Children.Clear();
            pipeLine = new PipeLine(30);
            pipeRender = new PipeRender(pipeLine);
            DesignPointInit = false;
        }
        /// <summary>
        /// 清除辅助点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClearAssPoints_Click(object sender, RoutedEventArgs e)
        {
            win.Children.Clear();
        }
        /// <summary>
        /// 重新渲染
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Rerend_Click(object sender, RoutedEventArgs e)
        {
            if (pipeRender != null)
            {
                Render();
                //PathGeometry pathGeometry = pipeRender.Render(win);
                //if (pathGeometry != null)
                //{
                //    box.Clip = null;
                //    box.Clip = pathGeometry;
                //}
            }
        }

        bool NextDesign = true;
        bool DesignPointInit = false;
        Point DesignPoint = new Point();
        /// <summary>
        /// 鼠标在设计界面的移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void box_MouseMove(object sender, MouseEventArgs e)
        {
            //return;
            if (pipeLine != null && NextDesign)
            {
                if (pipeLine.Count > 0)
                {
                    Point point = e.GetPosition(this);
                    point.X = Math.Round(point.X);
                    point.Y = Math.Round(point.Y);
                    PipeEndPoint pipeEndPoint = new PipeEndPoint(point);
                    if (!DesignPointInit)
                    {
                        pipeLine.Add(pipeEndPoint);
                        DesignPointInit = true;
                    }
                    pipeLine[pipeLine.Count - 1] = pipeEndPoint;
                    PathGeometry pathGeo = Render();
                    //PathGeometry pathGeometry = pipeRender.Render(win);
                    //if (pathGeometry != null)
                    //{
                    //    box.Clip = null;
                    //    box.Clip = pathGeometry;
                    //}
                }
            }
        }
        /// <summary>
        /// 鼠标移出设计界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void box_MouseLeave(object sender, MouseEventArgs e)
        {
            if (NextDesign && pipeLine != null && pipeLine.Count > 1)
            {
                pipeLine.Remove(pipeLine.Count - 1);
            }
            DesignPointInit = false;
        }
        /// <summary>
        /// 右键取消管道设计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (DesignPointInit && pipeLine.Count > 0)
            {
                pipeLine.Remove(pipeLine.Count - 1);
                DesignPointInit = false;
                NextDesign = false;
                Render();
            }
        }

        /// <summary>
        /// 添加一个管道关节
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Point point = e.GetPosition(this);
            if (DesignPointInit)
            { pipeLine.Insert(pipeLine.Count - 1, new PipeEndPoint(point)); }
            else
            { pipeLine.Add(new PipeEndPoint(point)); }

            Render();
            NextDesign = true;
        }

        private PathGeometry Render()
        {
            PathGeometry pathGeometry = pipeRender.Render(win, box);
            if (pathGeometry != null)
            {
                box.Clip = null;
                box.Clip = pathGeometry;

                //GradientStopCollection RecGsc = new GradientStopCollection();
                //RecGsc.Add(new GradientStop(Color.FromArgb(180,255, 0, 0), 0));
                //RecGsc.Add(new GradientStop(Color.FromRgb(244, 244, 244), 0.5));
                //RecGsc.Add(new GradientStop(Color.FromRgb(80, 80, 80), 1));

                //box.Background = null;
                ////box.Background = new LinearGradientBrush(RecGsc, new Point(pipeLine[1].CalcPoints[0].X, pipeLine[1].CalcPoints[0].Y)
                ////    , new Point(pipeLine[1].CalcPoints[2].X, pipeLine[1].CalcPoints[2].Y));
                //box.Background = new LinearGradientBrush(RecGsc, new Point(0, 0)
                //    , new Point(0, 1));
            }
            return pathGeometry;
        }
    }
}
